package com.sskj.uboex.ui.activity;


import android.text.InputFilter;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.box.inputfilter.MoneyValueFilter;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.mine.bean.CoinFeeBean;
import com.sskj.uboex.R;
import com.sskj.uboex.component.DaggerUserDataComponent;
import com.sskj.uboex.presenter.WithdrawActivityPresenter;

import java.math.BigDecimal;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

@Route(path = RConfig.APP_WITHDRAW)//提币
public class WithdrawActivity extends BaseActivity<WithdrawActivityPresenter> {
    @BindView(R.id.etAddress)
    EditText etAddress;
    @BindView(R.id.tvUSDT)
    TextView tvUSDT;
    @BindView(R.id.tvFee)
    TextView tvFee;
    @BindView(R.id.etPwd)
    EditText etPwd;
    @BindView(R.id.etCode)
    EditText etCode;
    @BindView(R.id.btGetCode)
    Button btGetCode;
    @BindView(R.id.btSubmit)
    Button btSubmit;
    @BindView(R.id.etNum)
    EditText etNum;
    @BindView(R.id.tvAll)
    TextView tvAll;
    @Autowired
    String code;
    @Autowired
    String money;
    String minCoin;
    String maxCoin;
    private DisposableSubscriber<Long> disposableSubscriber;
    private String mTb_fee;
    private Double wallone;
    @Inject
    UserViewModel userViewModel;
    private String tel;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_withdraw;
    }

    @Override
    public WithdrawActivityPresenter getPresenter() {
        return new WithdrawActivityPresenter();
    }

    @Override
    protected void initView() {
        ARouter.getInstance().inject(this);
        RxBus.getDefault().register(this);
        DaggerUserDataComponent.create().inject(this);
        setTitle(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity0));

        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                tel = users.get(0).getTel();
                ClickUtil.click(btGetCode, () -> {
                    mPresenter.sendCode(tel);
                });
            }
        });
        tvUSDT.setText(money + " " + code);
        ClickUtil.click(btSubmit, () -> {
            if (TextUtils.isEmpty(etAddress.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity1));
                return;
            }
            if (TextUtils.isEmpty(etNum.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity2));
                return;
            }
            Double aDouble = Double.valueOf(etNum.getText().toString());
            if (aDouble < Double.valueOf(minCoin)) {
                ToastUtil.showShort(String.format(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity3), minCoin));
                return;
            }

            if (aDouble > Double.valueOf(maxCoin)) {
                ToastUtil.showShort(String.format(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity4), maxCoin));
                return;
            }

            if (aDouble > new BigDecimal(wallone).add(new BigDecimal("-" + mTb_fee).divide(new BigDecimal("100"))).doubleValue()) {
                ToastUtil.showShort(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity7));
                return;
            }

            if (TextUtils.isEmpty(etPwd.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity8));
                return;
            }
            if (TextUtils.isEmpty(etCode.getText())) {
                ToastUtil.showShort(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity9));
                return;
            }
            mPresenter.withdraw(etCode.getText().toString().trim(),
                    etPwd.getText().toString().trim(),
                    etNum.getText().toString().trim(),
                    etAddress.getText().toString().trim(),
                    code
            );
        });

        etNum.setFilters(new InputFilter[]{new MoneyValueFilter().setDigits(4)});
        mPresenter.getWithdrawFee(code);
    }

    public void startCount() {
        btGetCode.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                btGetCode.setText(time + "s");
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                btGetCode.setText(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity11));
                btGetCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    public void updateCoin(CoinFeeBean data) {
        wallone = Double.valueOf(money);
        mTb_fee = data.getTb_fee();
        tvUSDT.setText(String.format("(可用%s)", money));
        ClickUtil.click(tvAll, () -> {
            BigDecimal allBig = new BigDecimal(wallone).add(new BigDecimal("-" + data.getTb_fee()));
            if (allBig.doubleValue() <= 0) {
                etNum.setText("0");
            } else {
                etNum.setText(NumberUtil.keep4(allBig.toPlainString()));
            }

        });
        tvFee.setText(mTb_fee);
        minCoin = data.getTb_min();
        maxCoin = data.getTb_max();
        etNum.setHint(String.format(App.INSTANCE.getString(com.sskj.mine.R.string.mineWithdrawActivity15), data.getTb_min(), data.getTb_max()));
    }

    @Override
    protected void onDestroy() {
        RxBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public void withDrawSuccess() {
        userViewModel.update();
        finish();
    }


}
